﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AiLuWeb.WebSite.Code;
using System.Collections;
using AiLuWeb.Model;
using AiLuWeb.Business;
using Ajeelee.Core.Utils;

namespace AiLuWeb.WebSite.WebAdmin
{
    public partial class SysUserEdit : AuthPageBase
    {
        protected int id;
        protected string type = "1";

        Hashtable table = new Hashtable();
        protected UAdministrator userInfo = new UAdministrator();
        protected IList<SysModule> sysModuleList = new List<SysModule>();

        ISysModuleBusiness sysModuleBll = new SysModuleBusinessFactory().CreateSysModuleBusiness();
        IUAdministratorBusiness userBll = new UAdministratorBusinessFactory().CreateUAdministratorBusiness();

        string password = "";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                id = QueryParamInt32("id");
                type = QueryParam("type", "1");
                if (id > 0) //更新
                {
                    userInfo = userBll.GetById(id);
                    if (userInfo == null)
                    {
                        userInfo = new UAdministrator();
                        MessageBoxGo("不存在该信息", "SysUsers.aspx");
                    }
                }
                if (type == "1" || type == "3")
                {
                    sysModuleList = sysModuleBll.GetAllSysModule("");
                }
                if (IsPost)
                {
                    // 更新
                    if (id > 0)
                    {
                        int rel = 0;
                        if (type == "2")      // 修改密码
                        {
                            if (password == FormParam("oldpassword").MD5Secret())
                            {
                                if (FormParam("newpassword") == FormParam("dnewpassword"))
                                {
                                    table.Clear();
                                    table.Add(UAdministratorHash.Pwd, FormParam("newpassword").MD5Secret());
                                    table.Add(UAdministratorHash.Id, id);
                                    rel = userBll.UpdateSomeVal(table);
                                }
                                else
                                {
                                    MessageBox("两次密码不一样！");
                                    return;
                                }
                            }
                            else
                            {
                                MessageBox("老密码密码错误！");
                                return;
                            }
                        }
                        else if (type == "3") // 修改权限
                        {
                            table.Clear();
                            table.Add(UAdministratorHash.ModuleIds, Getmodule(FormParam("ckModuleid")));
                            table.Add(UAdministratorHash.RealName, FormParam("uprealname"));
                            table.Add(UAdministratorHash.Id, id);
                            rel = userBll.UpdateSomeVal(table);
                        }
                        if (rel > 0)
                        {
                            System.Threading.Thread.Sleep(500);
                            Page_GoTo("SysUsers.aspx", true);
                        }
                        else
                        {
                            MessageBox("修改失败！");
                            return;
                        }
                    }
                    else // 新建
                    {
                        if (type == "1")      // 新建
                        {
                            // 先检查一下
                            if (FormParam("password") == FormParam("dpassword"))
                            {
                                UAdministrator tempUserInfo = userBll.GetByName(FormParam("username"));
                                if (tempUserInfo != null || FormParam("username").ToUpper() == System.Configuration.ConfigurationManager.AppSettings["SuperuserName"].ToUpper())
                                {
                                    MessageBox("该用户名[" + FormParam("username") + "]已经存在了！");
                                }
                                else
                                {
                                    userInfo = new UAdministrator();
                                    userInfo.Name = FormParam("username");
                                    userInfo.RealName = FormParam("realname");
                                    userInfo.Pwd = FormParam("password").MD5Secret();
                                    userInfo.ModuleIds = Getmodule(FormParam("ckModuleid"));
                                    userInfo.State = 2;
                                    userInfo.LoginIP = IPAddress;
                                    userInfo.LoginTime = DateTime.Now;

                                    object rel = userBll.Insert(userInfo);

                                    System.Threading.Thread.Sleep(500);
                                    Page_GoTo("SysUsers.aspx", true);

                                }
                            }
                            else
                            {
                                MessageBox("两次密码不一样！");
                                return;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        private string Getmodule(string module)
        {
            if (module.IsNotNullAndEmpty())
            {
                List<string> pidm = new List<string>();
                foreach (string str in module.Split(","))
                {
                    if (str.Length >= 4 && !pidm.Contains(str.Substring(0, 2)))
                    {
                        pidm.Add(str.Substring(0, 2));
                    }
                }
                foreach (string str in pidm)
                {
                    module += "," + str;
                }
            }
            return module;
        }
    }
}